<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: zzh
  Date: 2022/4/20
  Time: 1:41 PM
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>学校管理</title>
</head>
<body>
<div>
    <center>
        <div>
            <div><h2>${title}</h2></div>
            <div>
                <div>学校管理</div>
                <div>
                    <button onclick="addSchool()">添加学校</button>
                </div>
                <br>
                <div>
                    <div style="display: none" id="addSchoolDiv">
                        <table>
                            <tr>
                                <th>添加学校</th>
                            </tr>
                            <tr>
                                <th>学校名:<input type="text" id="addSchoolName"></th>
                            </tr>
                        </table>
                        <div>
                            <button onclick="addSchoolTrue()">确认添加</button>
                        </div>
                    </div>
                </div>
                <div>
                    <div style="display: none" id="changeSchoolDiv">
                        <div>
                            <table>
                                <tr>
                                    <th>修改学校</th>
                                </tr>
                                <tr>
                                    <th>学校名:<input type="text" id="changeSchoolName"></th>
                                </tr>
                            </table>
                        </div>
                        <div>
                            <button onclick="changeSchoolTrue()">确认修改</button>
                        </div>
                    </div>
                </div>
                <div>
                    <table border="1">
                        <tr>
                            <th>学校id</th>
                            <th>学校名称</th>
                            <th>操作</th>
                        </tr>
                        <c:forEach items="${tSchoolList}" var="tSchool">
                            <tr>
                                <th>${tSchool.schoolId}</th>
                                <th>${tSchool.schoolName}</th>
                                <th>
                                    <table>
                                        <tr>
                                            <th>
                                                <button onclick="changeSchool(${tSchool.schoolId},'${tSchool.schoolName}')">
                                                    更改学校
                                                </button>
                                            </th>
                                        </tr>
                                    </table>
                                </th>
                            </tr>
                        </c:forEach>
                    </table>
                </div>
            </div>
        </div>
    </center>
</div>
</body>
<script type="text/javascript" src='<c:url value="/static/js/jquery.js" />'></script>
<script type="text/javascript">
    let schoolId = 0;

    function addSchool() {
        let addSchoolDiv = document.getElementById("addSchoolDiv");
        if (addSchoolDiv.style.display === "block") {
            addSchoolDiv.style.display = "none";
        } else {
            addSchoolDiv.style.display = "block";
        }
    }


    function changeSchool(schoolId1, schoolName) {
        let addSchoolDiv = document.getElementById("changeSchoolDiv");
        if (addSchoolDiv.style.display === "block") {
            addSchoolDiv.style.display = "none";
        } else {
            document.getElementById("changeSchoolName").value = schoolName
            schoolId = schoolId1
            addSchoolDiv.style.display = "block";
        }
    }

    function addSchoolTrue() {
        let addSchoolName = document.getElementById("addSchoolName").value;
        if (addSchoolName.length > 0) {
            if (confirm("确认添加学校？")) {
                var settings = {
                    "url": "${url}" + "/api/school/add",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "token": sessionStorage.getItem("token"),
                        "Content-Type": "application/json"
                    },
                    "data": JSON.stringify({
                        "schoolName": addSchoolName
                    }),
                };

                $.ajax(settings).done(function (response) {
                    if (response.status === 200) {
                        alert("添加成功");
                        history.go(0);
                    } else {
                        alert(response.data.error)
                    }
                });
            }
        } else {
            alert("请输入学校名称！")
        }
    }

    function changeSchoolTrue() {
        let changeSchoolName = document.getElementById("changeSchoolName").value
        if (changeSchoolName.length > 0) {
            if (confirm("确认修改学校名称？")) {
                var settings = {
                    "url": "${url}" + "/api/school/change",
                    "method": "POST",
                    "timeout": 0,
                    "headers": {
                        "token": sessionStorage.getItem("token"),
                        "Content-Type": "application/json"
                    },
                    "data": JSON.stringify({
                        "schoolId": schoolId,
                        "schoolName": changeSchoolName
                    }),
                };

                $.ajax(settings).done(function (response) {
                    if (response.status === 200) {
                        alert("修改成功");
                        history.go(0);
                    } else {
                        alert(response.data.error)
                    }
                });
            }
        } else {
            alert("请输入学校名称")
        }
    }
</script>
</html>
